package com.core.excel;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import com.core.excel.impl.ExcelEmployeeServiceImpl;
import com.core.exception.StandardException;
import com.core.util.DspUtil;

public class ReadExcel {

	private InputStream is;

	private Workbook workbook;

	private Sheet[] sheets;

	/**
	 * 读取excel数据
	 * 
	 * @param path
	 *            文件路径
	 * @param code
	 *            操作类型
	 * @throws IOException
	 * @throws BiffException
	 */
	public String run(String path, String code) throws Exception{
		if (DspUtil.isExl(path) == false) {
			return "文件格式不支持";
		}
		List<Map<String, Object>>  errorData = null;
		try {
			is = JxlObject.getInputStream(path);
			workbook = JxlObject.getWorkBook(is);
			sheets = JxlObject.getSheets(workbook);
			List<String> columnName = ExcelTemplatesByCode.getTemplates(code);
			String message = "";
			errorData = getCellDatas(code, sheets, columnName, message);
		} finally {
			sheets = null;
			workbook.close();
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return "{success:"+ DspUtil.isEmptyList(errorData) + " , errorPath:'"+path+"'}";//DspUtil.isEmptyList(errorData) ? "" : "false";
	}

	/**
	 * 读取单元格数据
	 * 
	 * @param sheets
	 * @param message
	 * @return
	 * @throws StandardException
	 * @throws IOException
	 * @throws BiffException
	 */
	private List<Map<String, Object>> getCellDatas(String code, Sheet[] sheets, List<String> columnName, String message)
			throws Exception, BiffException, IOException {
		int sCount = sheets.length;
		if (sCount < 1)
			throw new StandardException("excel 中没有可用的sheet");
		Sheet sheet = null;
		List<Map<String, Object>> data = null;
		ExcelOperateService eos = null;
		List<Map<String, Object>> errorData = null;
		if ("EMP".equalsIgnoreCase(code)) {
			eos = new ExcelEmployeeServiceImpl();
		}
		for (int index = 0; index < sCount; index++) {
			sheet = JxlObject.getSheet(workbook, index);
			int rows = sheet.getRows();
			if (rows < 2)
				throw new Exception("excel 中没有可导入的数据");
			data = new ArrayList<Map<String, Object>>();
			getCellDatas_0(sheet, rows, columnName, data);
			if (eos != null) {
				errorData = eos.run(data);
			}
		}
		return errorData;
	}

	/**
	 * 封装数据
	 * 
	 * @param sheet
	 *            Excel中的sheet
	 * @param rows
	 *            多少条记录
	 * @param columnName
	 *            列名对象
	 * @param data
	 *            存放读取的数据
	 * @return
	 */
	private String getCellDatas_0(Sheet sheet, int rows, List<String> columnName, List<Map<String, Object>> data) {
		for (int i = 2; i < rows; i++) {
			if (JxlObject.checkSheetIsNull(sheet))
				break;
			Map<String, Object> map = new HashMap<String, Object>();
			for (int j = 0; j < columnName.size(); j++) {
				String content = JxlObject.getCellValue(JxlObject.getCell(sheet, j, i));
				if (DspUtil.isEmpty(content) && map.isEmpty() == true)
					break; // 如果表头中没有这个数据,那就不要继续循环了
				map.put(columnName.get(j), content);
			}
			if (map.isEmpty() == false) {
				data.add(map);
			}
		}
		return "";
	}
}
